<?php
namespace app\models;
use yii\base\Model;

class MemberSearch extends Member
{
    public $begin_time;
    public $end_time;

    public function rules()
    {
        return [
            ['nickname', 'string', 'message' => 'nickname必须是字符串', 
                                   'max' => 18, 'tooLong' => 'nickname最长18位'],
            ['begin_time', 'date', 'format'=>'yyyy-MM-dd', 'message' => 'begin_time非yyyy-MM-dd格式'],
            ['end_time', 'date', 'format'=>'yyyy-MM-dd', 'message' => 'end_time非yyyy-MM-dd格式'],

        ];
    }

    public function scenarios()
    {
        return Model::scenarios();
    }

    public function search($params)
    {
        $data['MemberSearch'] = $params;
        // P($data);

        $query = Member::find();

        if (!($this->load($data) && $this->validate())) {
            // if($this->hasErrors()){
            //     P($this->getErrors());
            // }
            return false;
        }
        // P($this);

        if(isset($this->nickname)){
            $query -> andFilterWhere(
                ['like', 'nickname', $this->nickname]
            );
        }

        if(isset($this->end_time) and !empty($this->end_time)){
            $this->end_time .= ' 23:59:59';
        }
        $query -> andFilterWhere(['>=', 'add_time', $this->begin_time])
               -> andFilterWhere(['<=', 'add_time', $this->end_time]);

        // P($query);
        return $query;
    }

    
}